// Utility.keyConvert();
// const smcId = hardware.smartCard.serialNumber;
// EVM.cookie.setValue('smcId', smcId);
var indexR = 0;
var bealoonTable = false;
var data = [];

var itemid;
//旋转角度
var angles;
//可抽奖次数
var clickNum = 1;
//旋转次数
var rotNum = 0;
//中奖公告
var notice = null;
//转盘初始化 
var color = ["#f5e58a", "#d8aa4f ", "#a80800", "#DCC722", "white", "#FF4350"];
var info = ["谢谢参与", "三等奖", "谢谢参与", "三等奖", "三等奖", "二等奖", "三等奖", "一等奖"];
var info1 = ["谢谢参与", "三等奖", "谢谢参与", "三等奖", "三等奖", "二等奖", "三等奖", "一等奖"];
canvasRun();

function turntableclick() {
    if (clickNum >= 1) {
        //可抽奖次数减一
        clickNum = clickNum - 1;
        //转盘旋转
        runCup();
        //转盘旋转过程“开始抽奖”按钮无法点击
        $('#tupBtn').attr("disabled", true);
        //旋转次数加一
        rotNum = rotNum + 1;
        //“开始抽奖”按钮无法点击恢复点击
        setTimeout(function () {
            tableAlert(notice,itemid);
            $('#tupBtn').removeAttr("disabled", true);
            // cmsIndex.handle('alertItem')
            // alertItem.toggleFocus()
        }, 6000);
    } else {
        alert("亲，抽奖次数已用光！");
    }
}
//转盘旋转
function runCup() {
    probability();
    var degValue = 'rotate(' + angles + 'deg' + ')';
    $('#xttblog').css('-o-transform', degValue); //Opera
    $('#xttblog').css('-ms-transform', degValue); //IE浏览器
    $('#xttblog').css('-moz-transform', degValue); //Firefox
    $('#xttblog').css('-webkit-transform', degValue); //Chrome和Safari
    $('#xttblog').css('transform', degValue);
}

//各奖项对应的旋转角度及中奖公告内容
function probability() {
    //获取随机数
    var num = parseInt(Math.random() * (7 - 0 + 0) + 0);
    itemid = num;
    //概率
    if (num == 0) {
        angles = 2160 * rotNum + 1800;
        notice = info[0] + info1[0];
    }
    //概率
    else if (num == 1) {
        angles = 2160 * rotNum + 1845;
        notice = info[7] + info1[7];
    }
    //概率
    else if (num == 2) {
        angles = 2160 * rotNum + 1890;
        notice = info[6] + info1[6];
    }
    //概率
    else if (num == 3) {
        angles = 2160 * rotNum + 1935;
        notice = info[5] + info1[5];
    }
    //概率
    else if (num == 4) {
        angles = 2160 * rotNum + 1980;
        notice = info[4] + info1[4];
    }
    //概率
    else if (num == 5) {
        angles = 2160 * rotNum + 2025;
        notice = info[3] + info1[3];
    }
    //概率
    else if (num == 6) {
        angles = 2160 * rotNum + 2070;
        notice = info[2] + info1[2];
    }
    //概率
    else if (num == 7) {
        angles = 2160 * rotNum + 2115;
        notice = info[1] + info1[1];
    }
}

//绘制转盘
function canvasRun() {
    var canvas = document.getElementById('xttblog');
    var canvas01 = document.getElementById('xttblog01');
    var canvas03 = document.getElementById('xttblog03');
    var canvas02 = document.getElementById('xttblog02');
    var ctx = canvas.getContext('2d');
    var ctx1 = canvas01.getContext('2d');
    var ctx3 = canvas03.getContext('2d');
    var ctx2 = canvas02.getContext('2d');
    createCircle();
    createCirText();
    initPoint();

    //外圆
    function createCircle() {
        var startAngle = 0; //扇形的开始弧度
        var endAngle = 0; //扇形的终止弧度
        //画一个8等份扇形组成的圆形
        for (var i = 0; i < 8; i++) {
            startAngle = Math.PI * (i / 4 - 1 / 8);
            endAngle = startAngle + Math.PI * (1 / 4);
            ctx.save();
            ctx.beginPath();
            ctx.arc(345, 345, 150, startAngle, endAngle, false);
            ctx.lineWidth = 400;
            if (i % 2 == 0) {
                ctx.strokeStyle = color[0];
            } else {
                ctx.strokeStyle = color[1];
            }
            ctx.stroke();
            ctx.restore();
        }
    }

    //各奖项
    function createCirText() {
        ctx.textAlign = 'start';
        ctx.textBaseline = 'middle';
        ctx.fillStyle = color[3];
        var step = 2 * Math.PI / 8;
        for (var i = 0; i < 8; i++) {
            ctx.save();
            ctx.beginPath();
            ctx.translate(345, 345);
            ctx.rotate(i * step);
            ctx.font = "Bold 28px Microsoft YaHei";
            ctx.fillStyle = color[2];
            ctx.fillText(info[i], -30, -225, 60);
            ctx.font = "Bold 28px Microsoft YaHei";
            // ctx.fillText(info1[i],-30,-185,60);
            ctx.closePath();
            ctx.restore();
        }
    }

    function initPoint() {
        //箭头指针
        ctx1.beginPath();
        ctx1.moveTo(200, 0);
        ctx1.lineTo(220, 40);
        ctx1.lineTo(205, 30);
        ctx1.lineTo(205, 200);
        ctx1.lineTo(195, 200);
        ctx1.lineTo(195, 30);
        ctx1.lineTo(180, 40);
        ctx1.lineTo(200, 0);
        ctx1.fillStyle = "red";
        ctx1.fill();
        ctx1.closePath();
        //中间小圆
        ctx3.beginPath();
        ctx3.arc(150, 150, 120, 0, Math.PI * 2, false);
        ctx3.fillStyle = "#ffff66";
        ctx3.fill();
        ctx3.lineWidth = 25;
        ctx3.strokeStyle = "#a80800";
        ctx3.stroke();
        ctx3.closePath();
        //小圆文字
        ctx3.font = "Bold 20px Microsoft YaHei";
        ctx3.textAlign = 'start';
        ctx3.textBaseline = 'middle';
        ctx3.fillStyle = color[4];
        ctx3.beginPath();
        ctx3.fillText('开始', 135, 140, 40);
        ctx3.fillText('抽奖', 135, 160, 40);
        ctx3.fill();
        ctx3.closePath();
        //中间圆圈
        ctx2.beginPath();
        ctx2.arc(75, 75, 55, 0, Math.PI * 2, false);
        ctx2.lineWidth = 220;
        ctx2.fillStyle = 'orange'
        // ctx2.fillStyle = color[2];
        ctx2.fill();
        ctx2.closePath();
    }
}


function tableAlert(str,id)
{
    var shield = document.createElement("DIV");
    shield.id = "shield";
    shield.style.position = "absolute";
    shield.style.left = "50%";
    shield.style.top = "50%";
    shield.style.width = "808px";
    shield.style.height = "598px";
    shield.style.marginLeft = "-404px";
    shield.style.marginTop = "-299px";
    shield.style.zIndex = "300";
    shield.style.backgroundImage = "url('./images/inchoose.png')";
    shield.style.backgroundRepeat = "no-repeat";

    var strHtml1="";
    strHtml1 +=  "<div style='position:absolute;left:420px;top:205px;font-size:30px;color:yellow'>"+str+"</div>"
    strHtml1 +="<div id='item' class='item'><input type='text' class='strm'>'<div class='strm'></div></div>";
    shield.innerHTML = strHtml1;


    var alertFram = document.createElement("DIV");
    alertFram.id="alertFram";
    alertFram.style.position = "absolute";
    alertFram.style.width = "808px";
    alertFram.style.height = "598px";
    alertFram.style.left = "50%";
    alertFram.style.top = "50%";
    alertFram.style.marginLeft = "-404px";
    alertFram.style.marginTop = "-299px";
    alertFram.style.textAlign = "center";
    alertFram.style.lineHeight = "150px";
    alertFram.style.zIndex = "300";
    alertFram.style.backgroundImage = "url('./images/nochoose.png')";
    alertFram.style.backgroundRepeat = "no-repeat"


    if(id==0 ||id==2){
        document.body.appendChild(alertFram);
    }else{
        document.body.appendChild(shield);
    }
    // document.body.appendChild(shield);
    this.doOk = function(){
        alertFram.style.display = "none";
        shield.style.display = "none";
    }
    alertFram.focus();
    document.body.onselectstart = function(){return false;};
}





//获取机顶盒或浏览器信息
var env = evm.getStbBrowser();

function cookie(k, v) {
    if (v != undefined && typeof v == 'string') {
        evm.cookie(k, v);
        return;
    }
    if (!!evm.cookie(k)) {
        return evm.cookie(k);
    }
}
/*
 * $cmsIndex首页视图模块
 * 依赖:
 */
var cmsIndex = (function () {
    var mod = {};

    //配置项
    var config = mod.config = {
        //开启cookie开关
        cookieSwitch: true,
        cookieKey: 'cmsIndex',
        //模拟数据配置
        mockData: {
            //URI查询标示符
            key: '[mdata]',
            //文件目录
            dir: '../source/mockData'
        },
        //调试输出开关
        debug: false
    };

    var localData = mod.localData = {
        indexSource: '[mdata]/index.js'
    };
    //接口数据缓存
    var jsonCache = {};

    //各模块焦点切换的配置
    var modules = mod.modules = [];

    //当前模块的索引值,被引用的模块
    moduleId = 0,
        module = null,
        moduleGuid = 0,
        moduleCount = -1;

    mod.addModule = function () {
        return moduleCount += 1;
    };

    //moduleGuid存取器
    mod.moduleGuid = function (id) {
        if (typeof id == 'number') moduleGuid = id;
        return moduleGuid;
    };

    if (config.cookieSwitch && config.cookieKey) {
        //read cookie
        var pageCookie = cookie(config.cookieKey);
        if (pageCookie) {
            pageCookie = pageCookie.split(',');

            if (pageCookie.length > 0) {
                var moduleCookie = mod.moduleCookie = pageCookie[0].split('.');

                if (pageCookie[1]) {
                    var viewportCookie = mod.viewportCookie = moduleCookie;
                    moduleCookie = mod.moduleCookie = pageCookie[1].split('.');

                }
                if (pageCookie[2]) {
                    var parentCookie = mod.parentCookie = pageCookie[2].split('.');
                }
            }
        }

        //清除cookie
        evm.cutCookie(config.cookieKey);
    }
    //添加视图按键管理器
    mod.addListener = function () {
        evm.controller.subscribe({
            type: 1,
            method: 'add',
            callback: mod.grabEvent,
            context: mod
        });
    };
    // console.log(mod)
    //移除视图按键管理器
    mod.removeListener = function () {
        evm.controller.subscribe({
            type: 1,
            method: 'remove',
            callback: mod.grabEvent,
            context: mod
        });
    };
    // console.log(module)
    //视图按键管理

    mod.grabEvent = function (key) {
        var me = mod;
        //拦截返回键,方法返回isBlock是1或0,默认0不拦截
        var isBlock = 0;

        if (module && module.grabEvent) {
            isBlock = module.grabEvent(key) || 0;
        }

        return isBlock;
    };
    //模块存取器
    mod.module = function (mID) {
        if (modules[mID]) {
            module = modules[mID].module || null;
        }

        return module;
    };

    //切换当前模块
    mod.handle = function (directive) {
        var me = mod;
        if (modules[moduleId].map &&
            (typeof modules[moduleId].map[directive] == 'number')) {

            moduleId = modules[moduleId].map[directive];

            return me.module(moduleId);
        }
    };
    mod.paint = function () {
        var me = mod;
        // getMessage();
        me.addListener();
        return me;
    };

    mod.run = function () {
        var me = mod;
        //默认moduleId = 0 导航栏
        //cookie得到焦点

        var nav = mod.module(moduleId);
        cmsIndex.handle('right');
        right.toggleFocus();
        //控制器管理按键
        evm.on(document, (!env.stb ? 'keydown' : (env.stb == 'iPanel' ?
            'irkeypress' : 'keydown')), evm.controller, evm);

        me.paint();

        return me;
    };

    return mod;

})();


/*
 * 右侧区 列表
 */

cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'right',
    modNum: cmsIndex.moduleGuid(),
    map: {
        left: 1,
        turntable: 2,
    }
};

var right = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function () {
    var mod = {},
        index = 0, //右侧选中地址位置
        listName = 'turntable-li',
        length = 3,
        containerId = 'right',
        checkCookie = true; //配置是否定位焦点到二级栏目，默认否
    mod.modName = 'huodong';
    mod.modNum = cmsIndex.moduleGuid();
    mod.toggleFocus = function (hasIndex) {
        var me = mod;
        var elem = evm.$('turntable-list').getElementsByClassName(listName);
        for (i = 0; i < length; i++) {
            elem[i].className = listName;
        }
        if (index >= 0) {
            elem[index].className = elem[index].className + ' active';
        }
        return me;
    };

    mod.clearCss = function () {
        var me = mod;
        elem = $('.' + listName);
        for (i = 0; i < 3; i++) {
            elem[i].className = listName;
        }
        return me;
    };
    //赋值


    mod.moveH = function (directive) {
        var me = mod;
        if (directive == "backwards") { //左
            if(!bealoonTable){
                right.clearCss();
                cmsIndex.handle('turntable');
                turntable.toggleFocus();
            }
        } else if (directive == 'forwards') { //右

        }
    };
    mod.moveU = function (directive) {
        var me = mod;
        if (directive == "up") { //上
            if (index > 0) {
                index--;
            }
            mod.toggleFocus();
        } else if (directive == 'down') { //下
            if (index < 2) {
                index++;
                mod.toggleFocus();
            } else if (index == 2) {
                right.clearCss();
                cmsIndex.handle('left');
                left.toggleFocus();
            }
        }
        return me;
    };
    mod.action = function (directive) {
        var me = mod;
        switch (directive) {
            case 'back':
                break;
            case 'enter':
                if (index == 0) {
                    alert(0)
                }
                if (index == 1) {
                    alert(1)
                }
                if (index == 2) {
                    alert(2)
                }
                break;
        }
        return me;
    };
    mod.grabEvent = function (key) {
        var me = mod;
        //拦截返回键,方法返回isBlock是1或0,默认0不拦截
        var isBlock = 0;
        switch (key) {
            case keymap.Enrich.UP:
            case keymap.iPanel.UP:
            case keymap.DVN.UP:
                me.moveU('up');
                break;
            case keymap.Enrich.DOWN:
            case keymap.iPanel.DOWN:
            case keymap.DVN.DOWN:
                me.moveU('down');
                break;
            case keymap.Enrich.LEFT:
            case keymap.iPanel.LEFT:
            case keymap.DVN.LEFT:
                me.moveH('backwards');

                break;
            case keymap.Enrich.RIGHT:
            case keymap.iPanel.RIGHT:
            case keymap.DVN.RIGHT:

                me.moveH('forwards');

                break;
            case keymap.SELECT:
            case keymap.SELECT2:
                me.action('enter');
                break;
            case keymap.Enrich.BACK:
            case keymap.iPanel.BACK:
            case keymap.DVN.BACK:
                me.action('back');
                break;
            case keymap.Inspur.EXIT:
            case keymap.iPanel.EXIT:
            case 0:
                window.location.href = "address.html";
                break;
        }
        return isBlock;
    };

    return mod;
})();

/*
 * 退出
 */

cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'left',
    modNum: cmsIndex.moduleGuid(),
    map: {
        right: 0,
        turntable: 2,
    }
};

var left = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function () {
    var mod = {},
        index = 0, //右侧选中地址位置
        listName = 'back',
        length = 1,
        containerId = 'back',
        checkCookie = true; //配置是否定位焦点到二级栏目，默认否
    mod.modName = 'left';
    mod.modNum = cmsIndex.moduleGuid();
    mod.toggleFocus = function () {
        var me = mod;
        var elem = evm.$('back').getElementsByClassName(listName);
        for (i = 0; i < length; i++) {
            elem[i].className = listName;
        }
        if (index >= 0) {
            elem[index].className = elem[index].className + ' active';
        }
        return me;
    };

    mod.clearCss = function () {
        var me = mod;
        elem = $('#back>.' + listName);
        for (i = 0; i < 1; i++) {
            elem[i].className = listName;
        }
        return me;
    };
    //赋值


    mod.moveH = function (directive) {
        var me = mod;
        if (directive == "backwards") { //左
            // left.clearCss();
            // cmsIndex.handle('right');

            // right.toggleFocus(1);

        } else if (directive == 'forwards') { //右
            // left.clearCss();
            // cmsIndex.handle('right');
            // right.toggleFocus(2);
        }
        return me;
    };
    mod.moveU = function (directive) {
        var me = mod;
        if (directive == "up") { //上
            left.clearCss();
            cmsIndex.handle('right');
            right.toggleFocus();
        } else if (directive == 'down') { //下
        }
        return me;
    };
    mod.action = function (directive) {
        var me = mod;
        switch (directive) {
            case 'back':
                break;
            case 'enter':
                alert("退出")
                break;
        }
        return me;
    };
    mod.grabEvent = function (key) {
        var me = mod;
        //拦截返回键,方法返回isBlock是1或0,默认0不拦截
        var isBlock = 0;
        switch (key) {
            case keymap.Enrich.UP:
            case keymap.iPanel.UP:
            case keymap.DVN.UP:
                me.moveU('up');
                break;
            case keymap.Enrich.DOWN:
            case keymap.iPanel.DOWN:
            case keymap.DVN.DOWN:
                me.moveU('down');
                break;
            case keymap.Enrich.LEFT:
            case keymap.iPanel.LEFT:
            case keymap.DVN.LEFT:
                me.moveH('backwards');

                break;
            case keymap.Enrich.RIGHT:
            case keymap.iPanel.RIGHT:
            case keymap.DVN.RIGHT:

                me.moveH('forwards');

                break;
            case keymap.SELECT:
            case keymap.SELECT2:
                me.action('enter');
                break;
            case keymap.Enrich.BACK:
            case keymap.iPanel.BACK:
            case keymap.DVN.BACK:
                me.action('back');
                break;
            case keymap.Inspur.EXIT:
            case keymap.iPanel.EXIT:
            case 0:
                window.location.href = "address.html";
                break;
        }
        return isBlock;
    };

    return mod;
})();









/*
 * 左侧 转盘
 */

cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'turntable',
    modNum: cmsIndex.moduleGuid(),
    map: {
        left: 1,
        right: 0,
        alertItem:3,
    }
};

var turntable = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function () {
    var mod = {},
        index = 0, //右侧选中地址位置
        listName = 'taoge_btn',
        length = 1,
        containerId = 'right',
        checkCookie = true; //配置是否定位焦点到二级栏目，默认否
    mod.modName = 'huodong';
    mod.modNum = cmsIndex.moduleGuid();
    mod.toggleFocus = function (hasIndex) {
        var me = mod;
        var elem = evm.$('button').getElementsByClassName(listName);
        for (i = 0; i < length; i++) {
            elem[i].className = listName;
        }
        if (index >= 0) {
            elem[index].className = elem[index].className + ' active';
        }
        return me;
    };

    mod.clearCss = function () {
        var me = mod;
        elem = $('.' + listName);
        for (i = 0; i < 1; i++) {
            elem[i].className = listName;
        }
        return me;
    };
    //赋值


    mod.moveH = function (directive) {
        var me = mod;
        if (directive == "backwards") { //左

        } else if (directive == 'forwards') { //右
            if(!bealoonTable){
                turntable.clearCss();
                cmsIndex.handle('right');
                right.toggleFocus();
            }
        }
    };
    mod.moveU = function (directive) {
        var me = mod;
        if (directive == "up") { //上

        } else if (directive == 'down') { //下

        }
        return me;
    };
    mod.action = function (directive) {
        var me = mod;
        switch (directive) {
            case 'back':
                break;
            case 'enter':
                bealoonTable = true;
                turntableclick();
                turntable.clearCss();
                break;
        }
        return me;
    };
    mod.grabEvent = function (key) {
        var me = mod;
        //拦截返回键,方法返回isBlock是1或0,默认0不拦截
        var isBlock = 0;
        switch (key) {
            case keymap.Enrich.UP:
            case keymap.iPanel.UP:
            case keymap.DVN.UP:
                me.moveU('up');
                break;
            case keymap.Enrich.DOWN:
            case keymap.iPanel.DOWN:
            case keymap.DVN.DOWN:
                me.moveU('down');
                break;
            case keymap.Enrich.LEFT:
            case keymap.iPanel.LEFT:
            case keymap.DVN.LEFT:
                me.moveH('backwards');

                break;
            case keymap.Enrich.RIGHT:
            case keymap.iPanel.RIGHT:
            case keymap.DVN.RIGHT:

                me.moveH('forwards');

                break;
            case keymap.SELECT:
            case keymap.SELECT2:
                me.action('enter');
                break;
            case keymap.Enrich.BACK:
            case keymap.iPanel.BACK:
            case keymap.DVN.BACK:
                me.action('back');
                break;
            case keymap.Inspur.EXIT:
            case keymap.iPanel.EXIT:
            case 0:
                window.location.href = "address.html";
                break;
        }
        return isBlock;
    };

    return mod;
})();


/*
 * 提示框
 */

cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'alertItem',
    modNum: cmsIndex.moduleGuid(),
    map: {
        left: 1,
        right: 0,
        turntable:2,
    }
};

var alertItem = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function () {
    var mod = {},
        index = 0, //选中地址位置
        listName = 'strm',
        length = 2,
        containerId = 'right',
        checkCookie = true; //配置是否定位焦点到二级栏目，默认否
    mod.modName = 'huodong';
    mod.modNum = cmsIndex.moduleGuid();
    mod.toggleFocus = function (hasIndex) {
        var me = mod;
        var elem = evm.$('item').getElementsByClassName(listName);
        for (i = 0; i < length; i++) {
            elem[i].className = listName;
        }
        if (index >= 0) {
            elem[index].className = elem[index].className + ' active';
        }
        if(index==0){
            $(".item input").focus()
        }else{
            $(".item input").blur()
        }
        return me;
    };


    mod.clearCss = function () {
        var me = mod;
        elem = $('.' + listName);
        for (i = 0; i < 2; i++) {
            elem[i].className = listName;
        }
        return me;
    };
    //赋值


    mod.moveH = function (directive) {
        var me = mod;
        if (directive == "backwards") { //左
            if(index==0){
                index
            }else{
                index--;
            }
            mod.toggleFocus();
        } else if (directive == 'forwards') { //右
            if(index==1){
                index
            }else{
                index++;
            }
            mod.toggleFocus();
        }
    };
    mod.moveU = function (directive) {
        var me = mod;
        if (directive == "up") { //上

        } else if (directive == 'down') { //下

        }
        return me;
    };
    mod.action = function (directive) {
        var me = mod;
        switch (directive) {
            case 'back':
                break;
            case 'enter':

                break;
        }
        return me;
    };
    mod.grabEvent = function (key) {
        var me = mod;
        //拦截返回键,方法返回isBlock是1或0,默认0不拦截
        var isBlock = 0;
        switch (key) {
            case keymap.Enrich.UP:
            case keymap.iPanel.UP:
            case keymap.DVN.UP:
                me.moveU('up');
                break;
            case keymap.Enrich.DOWN:
            case keymap.iPanel.DOWN:
            case keymap.DVN.DOWN:
                me.moveU('down');
                break;
            case keymap.Enrich.LEFT:
            case keymap.iPanel.LEFT:
            case keymap.DVN.LEFT:
                me.moveH('backwards');

                break;
            case keymap.Enrich.RIGHT:
            case keymap.iPanel.RIGHT:
            case keymap.DVN.RIGHT:

                me.moveH('forwards');

                break;
            case keymap.SELECT:
            case keymap.SELECT2:
                me.action('enter');
                break;
            case keymap.Enrich.BACK:
            case keymap.iPanel.BACK:
            case keymap.DVN.BACK:
                me.action('back');
                break;
            case keymap.Inspur.EXIT:
            case keymap.iPanel.EXIT:
            case 0:
                window.location.href = "address.html";
                break;
        }
        return isBlock;
    };

    return mod;
})();











cmsIndex.run();